package com.own.business.wallet.controller.user;

import com.alibaba.fastjson2.JSON;
import com.own.aop.login.LoginIgnore;
import com.own.business.pay.common.em.PayEnum;
import com.own.business.pay.core.service.BillHistoryService;
import com.own.business.wallet.business.entity.PayResultEntity;
import com.own.business.wallet.business.util.WalletBusinessUtil;
import com.own.business.wallet.controller.user.form.WalletPayResponseForm;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserWalletPayController
 *
 * @author chenxueli
 * @date 2023-08-28 16:29:02
 */
@Tag(name = "钱包支付" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "pay/balance")
public class UserWalletPayController extends BaseController {

    @Resource
    private BillHistoryService billHistoryService;
    @Resource
    private WalletBusinessUtil walletBusinessUtil;

    @LoginIgnore
    @PostMapping("callback")
    public ResultModel<Boolean> callback(
            @RequestBody WalletPayResponseForm form
    ) {
        var entity = JSON.parseObject(form.getContent(), PayResultEntity.class);
        billHistoryService.notifySuccess(PayEnum.Method.BALANCE, entity.getOutTradeNo(), JSON.toJSONString(entity));
        walletBusinessUtil.notify(entity.getOrder().getType(), entity.getOrder().getOutTradeNo(), entity.getOrder().getOutRefundNo());
        return R.success();
    }

}
